package com.qjdchina.sku.front.web.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.qjdchina.sku.model.vo.CategoryStatisticsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qjdchina.common.web.util.RestBody;
import com.qjdchina.commons.result.Result;
import com.qjdchina.sku.biz.CategoryManager;
import com.qjdchina.sku.cache.CategoryCache;
import com.qjdchina.sku.consts.WebConsts;
import com.qjdchina.sku.model.Category;
import com.qjdchina.sku.model.vo.CategoryMenuVO;
import com.qjdchina.sku.service.enums.SkuResultCode;

/**
 * Created by fankiwu on 2016/11/22.
 */
@Controller
@RequestMapping(value = "category")
public class CategoryController {

    @Autowired
    private CategoryCache categoryCache;

    @Autowired
    private CategoryManager categoryManager;
    /**
     * 获取品类树
     * @return
     */
    @RequestMapping(value = "getCategory",method = RequestMethod.GET)
    @ResponseBody
    public RestBody<List<CategoryMenuVO>> getCategory(){

        List<CategoryMenuVO> categorys = categoryCache.getCategorys();
        return new RestBody<List<CategoryMenuVO>>(SkuResultCode.SUCCESS, categorys);
    }

    /**
     * 获取所有的一级品类
     * @return
     */
    @RequestMapping(value = "getRootCategory",method = RequestMethod.GET)
    @ResponseBody
    public RestBody<List<Category>> getRootCategory(HttpSession session){
    	//获取当前厂家信息
		Integer skuCompanyId = (Integer) session.getAttribute(WebConsts.COMPANY_ID);
		//根据条件查询品类
        Result<List<Category>> allRootCategory = categoryManager.getAllRootCategoryByCompany(skuCompanyId);
        if(!allRootCategory.isSuccess()){
            return new RestBody<List<Category>>(allRootCategory.getResultCode());
        }

        return new RestBody<List<Category>>(SkuResultCode.SUCCESS, allRootCategory.getDataModel());
    }
    /**
     * 根据父品类查询所有的子品类
     * @param parentId
     * @return
     */
    @RequestMapping(value = "getChildCategorys",method = RequestMethod.GET)
    @ResponseBody
    public RestBody<List<Category>> getChildCategory(@RequestParam(value = "parentId",required = true) Integer parentId,HttpSession session){

    	Integer skuCompanyId = (Integer) session.getAttribute(WebConsts.COMPANY_ID);
        Result<List<Category>> allRootCategory = categoryManager.getChildCategorysByCompany(parentId,skuCompanyId);
        if(!allRootCategory.isSuccess()){
            return new RestBody<List<Category>>(allRootCategory.getResultCode());
        }

        return new RestBody<List<Category>>(SkuResultCode.SUCCESS, allRootCategory.getDataModel());
    }

    /**
     * 统计各大品类下部品数量
     *
     * @return
     */
    @RequestMapping(value = "categoryStatistics ",method = RequestMethod.GET)
    @ResponseBody
    public RestBody<List<CategoryStatisticsVO>> categoryProductStatistical() {
        List<Integer> parentIds = new ArrayList<>();
        parentIds.add(1584);
        parentIds.add(1586);
        parentIds.add(1587);

        Result<List<CategoryStatisticsVO>> result = categoryManager.categoryProductStatistical(parentIds);
        if (!result.isSuccess()) {
            return new RestBody<List<CategoryStatisticsVO>>(result.getResultCode());
        }

        return new RestBody<List<CategoryStatisticsVO>>(SkuResultCode.SUCCESS, result.getDataModel());
    }
}
